<template>
	<view class="dealerGoods-list">
		<u-toast ref="uToast" /><u-no-network></u-no-network>
		<u-navbar title="" safeAreaInsetTop fixed placeholder>
			<view class="coreshop-navbar-left-slot" slot="left">
				<u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
				<u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
				<u-icon name="home" size="22" @click="goHome"></u-icon>
			</view>
			<view slot="right">
			</view>
		</u-navbar>
		<view class="content">
			<view class="coreshop-padding-10 coreshop-margin-bottom-5 coreshop-bg-white coreshop-solid-bottom">
				<u-search placeholder="请输入商品名称关键字搜索" v-model="key" :show-action="true" action-text="搜索"
					:animation="false" @custom="doSearch()"></u-search>
			</view>
			<scroll-view class="scroll-box" scroll-y>
				<view v-if="list.length">
					<view class="goods-card" v-for="(item, key) in list" :key="key">
						<view class="c-m-r-10">
							<image class='itmImg' mode="aspectFill" :src="item.image"></image>
						</view>
						<view class="coreshop-display-flex coreshop-flex-sub coreshop-flex-direction">
							<view class="top">
								<view class="name">{{ item.name }}</view>
								<view class="price">¥{{ item.price }}</view>
							</view>
							<view class="bottom">
								<view class="coreshop-display-flex"><u--text
										text="分类:"></u--text>{{item.goodsCategoryName || ''}}</view>
								<view class="coreshop-display-flex"><u--text text="品牌:"></u--text>
									{{item.brandName || ''}}
								</view>
							</view>
						</view>
						<view><u-icon name="edit-pen-fill" :size="20" color="#3191fd" @click="toEdit(item)"></u-icon>
						</view>
					</view>
					<!-- 更多 -->
					<u-loadmore :status="loadStatus" :icon-type="iconType" :load-text="loadText" margin-top="20"
						margin-bottom="20" />
				</view>
				<!-- 无数据时默认显示 -->
				<view class="coreshop-emptybox" v-else>
					<u-empty :icon="$globalConstVars.apiFilesUrl+'/static/images/empty/address.png'" icon-size="150"
						text="暂无商品信息" mode="list"></u-empty>
				</view>
			</scroll-view>

			<!--按钮-->
			<!-- <view class="coreshop-bg-white coreshop-footer-fixed coreshop-foot-padding-bottom">class="floatingButton" -->
			<view class="floatingButton" @click="toAdd">
				<!-- <u-button class="coreshop-percent-100" type="error" size="normal" @click="toAdd">新增商品</u-button> -->
				<u-icon name="plus-circle-fill" color="#e54d42" size="45"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [], // 列表
				title: '商品列表',
				dealerId: '',
				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				iconType: 'flower',
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				currentPage: 1,
				key: '',
				searchData: {
					id: null,
					where: {},
					limit: 10,
					page: 1
				},
			}
		},
		onShow() {
			this.currentPage = 1;
			this.list = [];
			this.getDealerGoods();
		},
		onReachBottom() { // 加载更多
			if (this.loadStatus === 'loadmore') {
				this.getDealerGoods()
			}
		},
		//加载执行
		onLoad(options) {
			const {
				title,
				store
			} = options
			if (title) this.title = title
			this.dealerId = store;
			console.log(options);
		},
		methods: {
			//获取列表
			getDealerGoods() {
				let self = this;

				this.loadStatus = 'loading';
				this.$u.api.getDealerGoods(this.conditions()).then(res => {
					if (res.status) {
						let _list = res.data;
						self.list = [...self.list, ..._list]
						let brands = res.otherData;
						self.list.forEach((item, index) => {
							let br = brands.find(c => c.id == item.brandId);
							if (br != null)
								item.brandName = br.name;
						});

						if (res.code > self.list.length) {
							self.currentPage++
							self.loadStatus = 'loadmore'
						} else {
							self.loadStatus = 'nomore'
						}
					}
				})
			},
			//添加
			toAdd() {
				let store = {};
				store.dealerId = this.dealerId;
				this.$u.route('/pages/member/dealer/dealerGoods/index/index?store=' + JSON.stringify(store));
			},
			//编辑
			toEdit(item) {
				let store = item;
				store.dealerId = this.dealerId;
				this.$u.route('/pages/member/dealer/dealerGoods/index/index?store=' + JSON.stringify(store));
			},
			//搜索
			doSearch() {
				this.currentPage = 1;
				this.list = [];
				this.getDealerGoods();
			},
			// 统一返回筛选条件 查询条件 分页
			conditions() {
				var where = {};
				if (this.key != '') {
					where = {
						searchName: this.key
					};
				}
				this.searchData.where = where;
				this.searchData.id = this.dealerId;
				this.searchData.page = this.currentPage;

				let data = this.searchData;
				var newData = this.$u.deepClone(data);
				if (data.where) {
					newData.where = JSON.stringify(data.where);
				}
				return newData;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.goods-card {}
</style>